package com.jing.test.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

/**
 * @author jingshouyan
 * 11/29/18 5:29 PM
 */
@Configuration
public class AppConfig implements ApplicationRunner {


    public static final String URL = "jdbc:mysql://127.0.0.1:3306/DEMO_IM_USER?useUnicode=true&characterEncoding=utf8&useSSL=false&statementInterceptors=brave.mysql.TracingStatementInterceptor";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "abcd1234";
    public static final String DRIVER = "com.mysql.jdbc.Driver";

    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    DataSource dataSource() {
        return initDataSource();
    }

    private DataSource initDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(URL);
        config.setDriverClassName(DRIVER);
        config.setUsername(USERNAME);
        config.setPassword(PASSWORD);
        config.setMaximumPoolSize(20);
        config.setMinimumIdle(5);
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        HikariDataSource dataSource = new HikariDataSource(config);
        return dataSource;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        InterceptorTest test100 = new InterceptorTest(100);
//        InterceptorTest test200 = new InterceptorTest(200);
//        ActionInterceptorHolder.addServerInterceptor(test100);
//        ActionInterceptorHolder.addServerInterceptor(test200);
    }
}
